package com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.AccountBasic;
import com.smedi.ismedi.authoritymanagement.core.domain.repository.AccountBasicRepository;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.model.AccountBasicEntity;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.persistence.mybatis.mapper.AccountBasicMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
@AllArgsConstructor
public class AccountBasicRepositoryImpl extends ServiceImpl<AccountBasicMapper, AccountBasicEntity> implements AccountBasicRepository, IService<AccountBasicEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public AccountBasic selectByUserId(Long userId) {
        AccountBasicEntity row = this.getBaseMapper().selectById(userId);
        return dozerUtil.convertor(row, AccountBasic.class);
    }

    @Override
    public AccountBasic selectByPersonId(Long personId) {
        QueryWrapper<AccountBasicEntity> wrapper = new QueryWrapper();
        wrapper.eq("person_id", personId);
        AccountBasicEntity row = this.getBaseMapper().selectOne(wrapper);
        return dozerUtil.convertor(row, AccountBasic.class);
    }

    @Override
    public AccountBasic doInsert(AccountBasic data) {
        AccountBasicEntity userBasicEntity = dozerUtil.convertor(data, AccountBasicEntity.class);
        this.getBaseMapper().insert(userBasicEntity);
        return dozerUtil.convertor(userBasicEntity, AccountBasic.class);
    }

    @Override
    public AccountBasic doUpdate(AccountBasic data) {
        AccountBasicEntity userBasicEntity = dozerUtil.convertor(data, AccountBasicEntity.class);
        this.getBaseMapper().updateById(userBasicEntity);
        return dozerUtil.convertor(userBasicEntity, AccountBasic.class);
    }

    @Override
    public AccountBasic selectByUserKeyAndPersonId(String userKey, Long personId) {
        QueryWrapper<AccountBasicEntity> wrapper = new QueryWrapper();
        wrapper.eq("user_key", userKey)
                .eq("person_id", personId);
        AccountBasicEntity row = this.getBaseMapper().selectOne(wrapper);
        return dozerUtil.convertor(row, AccountBasic.class);
    }

    @Override
    public AccountBasic selectByMobileOrUserName(String callAccount) {
        QueryWrapper<AccountBasicEntity> wrapper = new QueryWrapper();
        wrapper.or().eq("mobile", callAccount)
                .or().eq("user_name", callAccount);
        AccountBasicEntity row = this.getBaseMapper().selectOne(wrapper);
        return dozerUtil.convertor(row, AccountBasic.class);
    }

    @Override
    public void doDelete(Long userId) {
        this.getBaseMapper().deleteById(userId);
    }
}